package br.ufrj.neg.contatos.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.tools.view.VelocityViewServlet;

import com.thoughtworks.xstream.XStream;

import br.ufrj.neg.constants.General;
import br.ufrj.neg.contatos.bean.ContatoBean;
import br.ufrj.neg.contatos.model.ContatoModel;

public class CadastrarContatoServlet extends VelocityViewServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response){
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("contatoAction");
		String idNegocio = request.getParameter("idNegocio");
		
		
		if( action.equals("salvar")){
			salvarContato(request,response);
			return;
		}
		else if ( action.equals("inserir")){
			inserirContato(request,response);
			return;
		}
		else if ( action.equals("excluir")){
			excluirContato(request, response);
			return;
		}
		else if (action.equals("visualizar")){
			visualizarContato(request,response);
			return;
		}
		
		
		/*try {
		
			//response.sendRedirect("preparacao.carregar?idNegocio=" + idNegocio );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	private void salvarContato(HttpServletRequest request, HttpServletResponse response){
		String idContato = General.getParameterUTF8(request, "idContato");
		
		String nome = General.getParameterUTF8(request, "nome_contato");
		String telefone1 = General.getParameterUTF8(request, "telefone_1_contato");
		String telefone2 = General.getParameterUTF8(request, "telefone_2_contato");
		String email = General.getParameterUTF8(request, "email_contato");
		String cargo = General.getParameterUTF8(request, "cargo_contato");
		String endereco = General.getParameterUTF8(request, "endereco_contato");
		
		ContatoModel cm = new ContatoModel();
		ContatoBean cb = cm.getContato( Integer.parseInt(idContato) );
		
		cb.setNome(nome);
		cb.setTelefone1(telefone1);
		cb.setTelefone2(telefone2);
		cb.setEmail(email);
		cb.setCargo(cargo);
		cb.setEndereco(endereco);
		
		cm.atualizarContato(cb);	
		
		try {
			PrintWriter out;
			response.setContentType("text/xml");
			out = response.getWriter();		
			
			out.write( getXMLResponse(cb) );
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	private void inserirContato(HttpServletRequest request, HttpServletResponse response){
		String idNegocio = request.getParameter("idNegocio");
		String nome = General.getParameterUTF8(request, "nome_contato");
		String telefone1 = General.getParameterUTF8(request, "telefone_1_contato");
		String telefone2 = General.getParameterUTF8(request, "telefone_2_contato");
		String email = General.getParameterUTF8(request, "email_contato");
		String cargo = General.getParameterUTF8(request, "cargo_contato");
		String endereco = General.getParameterUTF8(request, "endereco_contato");
		
		ContatoModel cm = new ContatoModel();
		ContatoBean cb = new ContatoBean();
		
		cb.setIdNegociacao( Integer.parseInt(idNegocio) );
		cb.setNome(nome);
		cb.setTelefone1(telefone1);
		cb.setTelefone2(telefone2);
		cb.setEmail(email);
		cb.setCargo(cargo);
		cb.setEndereco(endereco);
		
		cm.cadastrarContato(cb);	
		try {
			PrintWriter out;
			response.setContentType("text/xml");
			out = response.getWriter();		
			
			out.write( getXMLResponse(cb) );
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

	}
	
	private void excluirContato(HttpServletRequest request, HttpServletResponse response){
		String idContato = General.getParameterUTF8(request, "idContato");
		String idNegocio = request.getParameter("idNegocio");
		
		ContatoModel cm = new ContatoModel();
		ContatoBean cb = cm.getContato( Integer.parseInt(idContato) );
		cb.setIdNegociacao( Integer.parseInt( idNegocio ));
		
		cm.excluirContato(cb);
		
		try {
			PrintWriter out;
			response.setContentType("text/xml");
			out = response.getWriter();		
			
			out.write( getXMLResponse(cb) );
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	private void visualizarContato(HttpServletRequest request, HttpServletResponse response){
		String idContato = General.getParameterUTF8(request, "idContato");

		ContatoModel cm = new ContatoModel();
		ContatoBean cb = cm.getContato( Integer.parseInt(idContato) );
		
		try {

		PrintWriter out;
		response.setContentType("text/xml");
		out = response.getWriter();		
				
		out.write( getXMLResponse(cb) );
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	private String getXMLResponse(Object obj){
		ContatoBean cb = (ContatoBean) obj;
		
		XStream xStream = new XStream();
		String XML = xStream.toXML(cb);
		
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + XML;
	}
	
}
